package com.ip.student.service;

import com.ip.student.models.bo.ScoreCourseSaveBO;
import com.ip.student.models.dto.ScoreCourseDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ip.student.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ip.student.mapper.ScoreFinalMapper;
import com.ip.student.models.service.IScoreFinalService;
import com.ip.student.models.bo.ScoreFinalSaveBO;
import com.ip.student.models.dto.ScoreFinalDTO;

/**
 * 成绩信息控制器
 *
 * @author 张景奇
 * @Date 2025-11-06 13:58:26
 */
@DubboService
public class ScoreFinalServiceImpl extends ServiceImpl<ScoreFinalMapper, ScoreFinalDTO> implements IScoreFinalService{

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(ScoreFinalSaveBO saveBO) {
        ScoreFinalDTO dto = new ScoreFinalDTO();
        BeanUtils.copyProperties(saveBO,dto);

        // 如果前端传回的ID为null或0，则使用雪花算法生成ID
        if (saveBO.getScoreId() == null || saveBO.getScoreId() == 0) {
            Long id = ymlConfig.getPkId();
            // 确保生成的ID为正数且在Integer范围内
            if (id < 0) {
                id = Math.abs(id);
            }
            // 确保ID在Integer范围内
            if (id > Integer.MAX_VALUE) {
                id = id % Integer.MAX_VALUE;
            }
            // 确保ID不为0
            if (id == 0) {
                id = 1L;
            }
            dto.setScoreId(id.intValue());
        } else {
            dto.setScoreId(saveBO.getScoreId());
        }
        return this.insert(dto);
    }
}
